<template>
  <div>
    <graphvis
      :cusGraphConfig="myGraphConfig"
      :cusGraphData="myGraphData"
      @init="fetchData"
      @nodeEvent="nodeHandler"
      @linkEvent="linkHandler"
    ></graphvis>
  </div>
</template>

<script>
import Graphvis from '@/components/graphvis';
import { sampleConfig, sampleData } from '@/components/graphvis/data/sample.js';
export default {
  components: {
    Graphvis,
  },
  created() {
    this.fetchData();
  },
  data() {
    return {
      myGraphConfig: {},
      myGraphData: [],
    };
  },
  methods: {
    fetchData() {
      this.tryl('fetchData', sampleConfig, sampleData);
      this.myGraphConfig = sampleConfig;
      this.myGraphData = sampleData;
    },
    nodeHandler(type, node) {
      this.$message('接收到节点事件' + type);
      console.log(node);
    },
    linkHandler(type, oldV, newV) {
      this.$message('接收到连接事件' + type);
      console.log(oldV);
      console.log(newV);
    },
    tryl(name, config, data) {
      console.log('当前处理------------------------' + name);

      let customConfig;
      if (this.isStringified(config)) {
        console.log('---------配置已stringfy');
        customConfig = JSON.parse(config);
      } else if (this.isParsed(config)) {
        console.log('---------配置已parse');
        customConfig = JSON.parse(JSON.stringify(config));
      }
      console.log(customConfig);

      let customData;
      if (this.isStringified(data)) {
        console.log('---------数据已stringfy');
        customData = JSON.parse(data);
      } else if (this.isParsed(data)) {
        console.log('---------数据已parse');
        customData = JSON.parse(JSON.stringify(data));
      }
      console.log(customData);
      console.log('正在离库------------------------' + name);
    },
    isStringified(str) {
      try {
        var obj = JSON.parse(str);
        if (obj && typeof obj === 'object' && obj !== null) {
          return true;
        }
      } catch (err) {
        return false;
      }
    },
    isParsed(str) {
      try {
        var obj = JSON.stringify(str);
        if (obj && typeof obj === 'string' && obj !== null) {
          return true;
        }
      } catch (err) {
        return false;
      }
    },
  },
};
</script>

<style></style>
